package net.lzzy.practices.web.course;

import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Applicant;
import net.lzzy.practices.entities.Chapter;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.services.ChapterService;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.web.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author hzl on 2022/5/30.
 * Description:
 */
@CrossOrigin
@RestController
@RequestMapping(ApiConstant.ROUTE_CHAPTER_ROOT)
public class ChapterApiController {
    @Autowired
    public ChapterApiController(ChapterService service) {
        this.service = service;
    }

    private final ChapterService service;

    @GetMapping(ApiConstant.ROUTE_CHAPTER_TEACHER)
    private ApiResult getTeachersChapters(@RequestParam String token,@RequestParam(name = "id") UUID courseId){
        List<Chapter> chapters=service.getTeachersChapters(courseId,token);
        return transform(chapters);
    }

    private ApiResult transform(List<Chapter> chapters){
        if (chapters==null){
            return new ApiResult(false, Messages.WRONG_ID.toString(), null);
        }
        List<VmChapter>vmChapters=new ArrayList<>();
        chapters.forEach(chapter -> vmChapters.add(new VmChapter(chapter)));
        return new ApiResult(true,chapters.size()+"",vmChapters);
    }

    @GetMapping(ApiConstant.ROUTE_CHAPTER_STUDENT)
    private ApiResult getStudentsChapters(@RequestParam String token,@RequestParam(name = "id") UUID courseId){
        List<Chapter> chapters=service.getStudentsChapters(courseId,token);
        return transform(chapters);
    }


    public ApiResult addOrUpdate(UUID id, ChapterInfo info){
        String token =info.getToken();
        String name=info.getName();
        String desc=info.getDesc();
        String strCourseId =info.getCourseId();
        boolean open=info.isOpen();
        if (StringUtils.isEmpty(token)||
                StringUtils.isEmpty(name)||
                StringUtils.isEmpty(desc)||
                StringUtils.isEmpty(strCourseId)){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        boolean create=id==null;
        UUID courseId=UUID.fromString(strCourseId);
        Chapter chapter=new Chapter();
        chapter.setOpen(open);
        chapter.setName(name);
        chapter.setDescription(desc);
        VmChapter vmChapter;
        if (create){
            vmChapter=new VmChapter(service.add(courseId,chapter,token));
        }else {
            vmChapter=new VmChapter(service.update(id,chapter,token));
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), vmChapter);
    }

    @PostMapping(ApiConstant.ROUTE_CHAPTER_UPDATE)
    public ApiResult update(@PathVariable UUID id,@RequestBody ChapterInfo info){
        return  addOrUpdate(id, info);
    }

    @PostMapping(ApiConstant.ROUTE_CHAPTER_ADD)
    public ApiResult add(@RequestBody ChapterInfo info){

        return  addOrUpdate(null, info);
    }

    @PostMapping(ApiConstant.ROUTE_CHAPTER_OPEN_CLOSE)
    public ApiResult openCloseChapter(@RequestBody ChapterOpenInfo info){
        String token=info.getToken();
        String strChapterId=info.getChapterId();
        boolean open=info.isOpen();
        if (StringUtils .isEmpty(token)||StringUtils.isEmpty(strChapterId)){
            return new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);

        }
        Chapter chapter=service.openCloseChapter(UUID.fromString(strChapterId),open,token);
        if (chapter==null){
            return new ApiResult(false,Messages.NO_PERMISSION+"或"+Messages.WRONG_ID,null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(),new VmChapter(chapter));
    }
    @DeleteMapping(ApiConstant.ROUTE_CHAPTER_REMOVE)
    public ApiResult delete(@RequestBody ChapterKeyInfo info){
        String token=info.getToken();
        String strChapterId=info.getChapterId();
        if (StringUtils.isEmpty(token)||StringUtils.isEmpty(strChapterId)){
            return  new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
        }
        Messages msg=service.remove(UUID.fromString(strChapterId),token);
        return new ApiResult(msg==Messages.SUCCESS, msg.toString(), null);
    }
}
